using Stack;

public class LinkedStack<T> : IStack<T>
{
    public Node<T> Top { get; set; }
    public int Number { get; set; }//统计节点数量
    public LinkedStack()
    {
        Top = null;
        Number = 0;
    }
    public void Clear()
    {
        Top = null;
        Number = 0;
    }

    public int GetLength() => Number;

    public T GetTop()
    {
        if (IsEmpty())
        {
            System.Console.WriteLine("空链栈");
            return default(T);
        }
        return Top.Data;
    }

    public bool IsEmpty() => Top == null && Number == 0;

    public T Pop()
    {
        if (IsEmpty())
        {
            System.Console.WriteLine("空链栈");
            return default(T);
        }
        var p = Top;
        Top = Top.Next;
        --Number;//维护数量 
        return p.Data;
    }

    public void Push(T item)
    {
        var q = new Node<T>(item);
        if (Top == null)
        {
            Top = q;
        }
        else
        {
            q.Next = Top;
            Top = q;
        }
        ++Number;//维护Number字段
    }
}